﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ClassDAL.Entities;

namespace CodeGenBL.Dto
{

    [DataContract]
    public class AttributeModelDto
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public ClassModelDto ClassModel { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public bool Display { get; set; }
        [DataMember]
        public bool Search { get; set; }
        [DataMember]
        public bool SearchResult { get; set; }
        [DataMember]
        public string DataType { get; set; }
        [DataMember]
        public string FieldDescription { get; set; }
        [DataMember]
        public string Label { get; set; }
        [DataMember]
        public string FieldType { get; set; }
        [DataMember]
        public int Length { get; set; }
        [DataMember]
        public bool AutoKey { get; set; }
        [DataMember]
        public bool Nullable { get; set; }
        [DataMember]
        public bool Pk { get; set; }
        [DataMember]
        public int SeqNo { get; set; }
        [DataMember]
        public int DisplayLengthInGrid { get; set; }
    }

    public static class AttributeExt
    {
        public static List<AttributeModel> TransformToAttributes(this List<AttributeModelDto> attributes)
        {
            Microsoft.CSharp.CSharpCodeProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
            if (attributes != null)
            {
                foreach (var item in attributes)
                {
                    if (!prov.IsValidIdentifier(item.Name))
                    {
                        throw new Exception("Invalid Charecters");
                    }
                }
            }
            return attributes.Select(a => new AttributeModel
            {
                Id = a.Id,
                AutoKey = a.AutoKey,
                DataType = a.DataType,
                Display = a.Display,
                FieldDescription = a.FieldDescription,
                FieldType = a.FieldType,
                Label = a.Label,
                Length = a.Length,
                Name = a.Name,
                Nullable = a.Nullable,
                Pk = a.Pk,
                Search = a.Search,
                SearchResult = a.SearchResult,
                SeqNo = a.SeqNo,
                DisplayLengthInGrid = a.DisplayLengthInGrid
            }).ToList();
        } 
    }
}
